//
//  BirthdaySettingCell.m
//  18Birdies
//
//  Created by weihuazhang on 2016/11/30.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import "BirthdaySettingCell.h"
#import "LcFoundation.h"
#import "BirthdaySettingCellData.h"


@implementation BirthdaySettingCell

- (void)postContruct {
  [super postContruct];
//  UIDatePicker* datePicker = [[UIDatePicker alloc] init];
//  datePicker.datePickerMode = UIDatePickerModeDate;
//  datePicker.date = [self birthdayDateForPicker:self.textField.text];
//  datePicker.minimumDate = [self birthdayDateForPicker:@"01/01/1900"];
//  datePicker.maximumDate = [self birthdayDateForPicker:@"12/30/2099"];
//  self.textField.inputView = datePicker;
//  self.datePicker = datePicker;
//  [self initBirthdayToolBar];

}

- (void)updateDatePicker {
//  UIDatePicker* datePicker = (UIDatePicker*)self.textField.inputView;
//  datePicker.date = [self birthdayDateForPicker:self.textField.text];
}

- (void)setMaxDate:(NSDate*)date {
  self.datePicker.maximumDate = date;
}

- (void)initBirthdayToolBar {
  UIToolbar* toolbar = [[UIToolbar alloc] init];
  toolbar.backgroundColor = [UIColor lightGrayColor];
  
  CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
  toolbar.frame = CGRectMake(0, 0, screenW, 40);
  
  UIBarButtonItem* flexibleItem = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                   target:nil
                                   action:nil];
  UIBarButtonItem* doneItem =
  [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                   style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(birthdayPickerDone:)];
  toolbar.items = @[ flexibleItem, doneItem ];
  self.textField.inputAccessoryView = toolbar;
}

- (void)birthdayPickerDone:(id)sender {
  LC_CHECK_NOT_NULL(self.cellData);
  
  UIDatePicker* datePicker = (UIDatePicker*)self.textField.inputView;
  NSDate* date = datePicker.date;
  self.textField.text = [self birthdayStringForDate:date];
  
  BirthdaySettingCellData* cellData = (BirthdaySettingCellData*)self.cellData;
  cellData.content = self.textField.text;
  
  [self.textField resignFirstResponder];
}

- (NSString*)birthdayStringForDate:(NSDate*)date {
  return [date toStringWithFormat:@"MM/dd/yyyy"];
}

- (NSDate*)birthdayDateForPicker:(NSString*)birthday {
  if (birthday.length == 0) {
    birthday = @"01/01/1980";
  }
  return [NSDate dateFromString:birthday andFormat:@"MM/dd/yyyy"];
}

@end
